package com.sx.gongzhong.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.MyApplication;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.model.UserInfoBean;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.widget.CustomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * 设置
 * Created by admin on 2018/1/2.
 */

public class SettingActivity extends BaseActivity {

    @BindView(R.id.tv_logout)
    TextView tvLogout;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setTitleName("设置");
        if (MyApplication.instance.userBean != null && !BaseUtil.isEmpty(MyApplication.instance.userBean.sid)) {
            tvLogout.setText("退出登录");
        }else {
            tvLogout.setText("登录");
        }
    }

    @OnClick({R.id.rl_update_password, R.id.rl_hand_password, R.id.rl_feedback, R.id.rl_about_us,
            R.id.rl_Phone,R.id.tv_logout})
    public void onClick(View view) {
        if (view.getId() == R.id.rl_about_us) {
            ToastUtils.showToast(SettingActivity.this, "关于我们");
            return;
        }
        if (!isLogin()){
            return;
        }
        switch (view.getId()) {
            case R.id.rl_update_password:
                startActivity(new Intent(this, UpdatePasswordActivity.class));
                break;
            case R.id.rl_hand_password:
                startActivity(new Intent(this, HandPasswordActivity.class));
                break;
            case R.id.rl_feedback:
                startActivity(new Intent(this, FeedBackActivity.class));
                break;
            case R.id.tv_logout:
                createLogin();
                break;
            case R.id.rl_Phone:
                startActivity(new Intent(this, UpdatePhoneActivity.class));
                break;
        }
    }

    private void createLogin() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        CustomDialog dialog = builder.setMessage("确定退出当前账号？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MyApplication.instance.userBean = null;
                MyRxBus.get().post(new MsgEvent("MyFragment", 1, null));
                IDataStorage dataStorage = DataStorageFactory.getInstance(getApplicationContext(),
                        DataStorageFactory.TYPE_DATABASE);
                if (dataStorage.contains(UserInfoBean.class,getPackageName())){
                    UserInfoBean userBean = dataStorage.load(UserInfoBean.class,getPackageName());
                    userBean.sid = "";
                    dataStorage.storeOrUpdate(userBean,getPackageName());
                }
                JPushInterface.stopPush(MyApplication.context);
                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                finish();
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (tvLogout != null){
            if (MyApplication.instance.userBean != null && !BaseUtil.isEmpty(MyApplication.instance.userBean.sid)) {
                tvLogout.setText("退出登录");
            }else {
                tvLogout.setText("登录");
            }
        }
    }
}
